package com.munhrasoft.dinamica.business.util;

import java.io.File;
import java.io.IOException;

import com.munhrasoft.dinamica.business.entities.CheckListHeader;
import com.munhrasoft.dinamica.business.entities.CheckListLines;
import com.munhrasoft.dinamica.business.logic.CheckListEJB;

import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.read.biff.BiffException;

public class LoadCheckListData {

	private Workbook wk;
	public static final int HEADER_LINES = 5;
	
	private CheckListEJB checkListEJB;
    //private static EJBContainer ec;
    //private static Context ctx;

	/*
    private static void initContainer() {       
        System.out.println("initContainer");                
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put(EJBContainer.MODULES, new File("target/classes"));
        properties.put("org.glassfish.ejb.embedded.glassfish.installation.root", 
                "./src/test/glassfish");
        ec = EJBContainer.createEJBContainer(properties);
        ctx = ec.getContext();          
        try {
			checkListEJB = (CheckListEJB) ctx.lookup("java:global/classes/CheckListEJB");
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
   
    private static void closeContainer() {
        ec.close();
        System.out.println("AfterClass");
    }
	*/
	
	public LoadCheckListData(String filepath, CheckListEJB checkListEJB){
		
		try {
			WorkbookSettings ws = new WorkbookSettings();
			ws.setEncoding("Cp1252");
			wk = Workbook.getWorkbook(new File(filepath),ws);
		} catch (BiffException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void executeParser(){
		//initContainer();
		System.out.println("Numero de abas "+wk.getSheets().length);
		String tituloGrupo = new String();
		
		for (int abaCount = 0 ; abaCount < wk.getNumberOfSheets() ; abaCount ++){
			
			CheckListHeader chkHeader = new CheckListHeader();
			Sheet chkSheet = wk.getSheets()[abaCount];
			chkSheet.getCell(0, 0).getContents();
			System.out.println("Titulo "+chkSheet.getCell(0, 0).getContents());
			chkHeader.setTitulo(chkSheet.getCell(0, 0).getContents());
			
			System.out.println("Operacao "+chkSheet.getCell(1,1).getContents());
			System.out.println("Tipo "+chkSheet.getCell(1,2).getContents());
			System.out.println("Modalidade1 "+chkSheet.getCell(1,3).getContents());
			System.out.println("Modalidade2 "+chkSheet.getCell(1,4).getContents());
			
			chkHeader.setOperacao(chkSheet.getCell(1,1).getContents());
			chkHeader.setTipo(chkSheet.getCell(1,2).getContents());
			chkHeader.setModalidade1(chkSheet.getCell(1,3).getContents());
			chkHeader.setModalidade2(chkSheet.getCell(1,4).getContents());
			
			checkListEJB.createChkHeader(chkHeader);
			
			for (int linesCount = HEADER_LINES; linesCount < chkSheet.getRows() - HEADER_LINES; linesCount++){
				
				CheckListLines chkLine = new CheckListLines();
					
				if ("GRUPO".equals(chkSheet.getCell(1,linesCount).getContents())){
					tituloGrupo = new String( chkSheet.getCell(0,linesCount).getContents());
					chkLine.setDescricao(chkSheet.getCell(0,linesCount+1).getContents());
				}else{
					chkLine.setDescricao(chkSheet.getCell(0,linesCount).getContents());
				}
				
				chkLine.setGrupo(tituloGrupo);
	
				System.out.println("Grupo "+chkLine.getGrupo());
				System.out.println("Descrição "+chkLine.getDescricao());
				
				checkListEJB.addCheckLine(chkHeader, chkLine);
			}
			// N�o pode atualizar toda hora o updaate no ejb sen�o duplica o registro
			// precisa arrumar o teste do checklist
			checkListEJB.updateCheckListHeader(chkHeader);
		}
		//closeContainer();
	}

}
